package com.excellent.controller;

import com.excellent.cloudApi.Sample;
import com.excellent.service.ImageService;
import org.json.JSONException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
@CrossOrigin
@RestController
public class ImageController {

    @Value("${my.upload.dir}")
    private String uploadDir;

    @Autowired
    private ImageService imageService;


    //get请求，接收fileName参数，返回图片识别结果
    @GetMapping("/recognize")
    public String getImageResult(@RequestParam String fileName) throws IOException, JSONException {
//        开始计时
        long startTime = System.currentTimeMillis();
        // 这里只是示意，实际代码需要根据具体业务逻辑实现
        String result = Sample.recognize(uploadDir + fileName);
//        结束计时，计算耗时
        long endTime = System.currentTimeMillis();
        System.out.println("耗时：" + (endTime - startTime) + "ms");
        imageService.saveImageRecognization(fileName,result,endTime - startTime);
        return result;
    }


}
